﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.NetworkInformation;

namespace CLFramework.EnvironmentCheck.CheckItems
{
    public class NetWork : ICheckItem
    {
        public int Order
        {
            get { return 3; }
        }

        public string Name
        {
            get { return "网络连接"; }
        }

        public string Remarks
        {
            get {
                bool ok = result;
                if (!check)
                {
                    ok = Result;
                }

                if (ok)
                    return "延迟时间" + replytime.ToString() + "毫秒";
                else
                    return "您与服务器之间的网络连接超时或不正常"; 
            }
        }

        public bool Must
        {
            get { return true; }
        }

        private bool result = false;
        private bool check = false;
        public bool Result
        {
            get
            {
                if (!check)
                {
                    result = CheckNetwork("crm.kxtx.cn");
                }
                return result;
            }
        }

        long replytime = 0;
        bool CheckNetwork(string address)
        {
            try
            {
                PingReply reply = new Ping().Send(address);
                replytime = reply.RoundtripTime;
                return (reply.Status == IPStatus.Success);
            }
            catch
            {
                return false;
            }
        }
    }
}
